package com.block.datapush.form.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.common.model.vo.ResultInfoVO;
import com.block.datapush.form.enums.PushResultEnum;
import com.block.datapush.form.service.IFormCallBackService;
import com.block.datapush.form.service.IFormInfoHisService;
import com.block.datapush.form.service.IFormInfoProfitService;
import com.block.db.entity.FormInfo;
import com.block.db.entity.FormInfoHis;
import com.block.db.mapper.FormInfoHisMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class IFormCallBackServiceImpl implements IFormCallBackService {

    @Resource
    FormInfoHisMapper formInfoHisMapper;
    @Autowired
    IFormInfoProfitService formInfoProfitService;
    @Autowired
    IFormInfoHisService formInfoHisService;

    @Override
    public ResultInfoVO getMobileInfo(FormInfo formInfo, String orderNo){

        // 判断渠道状态
        if (StrUtil.isEmpty(orderNo)){
            return ResultInfoVO.error("订单号为空");
        }

        FormInfoHis formInfoHis = formInfoHisMapper.selectById(orderNo);
        if (ObjectUtil.isEmpty(formInfoHis) ||
                !StrUtil.equals(String.valueOf(formInfoHis.getFormId()), String.valueOf(formInfo.getId()))){
            return ResultInfoVO.error("订单信息错误");
        }

        if (StrUtil.equals(formInfoHis.getPushResult(), PushResultEnum.WAIT_CONFIRM.getType())){
            formInfoHisService.updateFormInfoHis(formInfoHis, PushResultEnum.SUCCESS, null, null);
        }

        JSONObject returnData = new JSONObject();
        returnData.put("mobile", formInfoHis.getMobile());

        return ResultInfoVO.success(returnData);
    }

}
